const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hytypelist:[],
    hyjglist:[],
    index:0,
    hytype:'基础会员',
    hyjg:'6800',
    hyzf:0,
    hyyear:1,
    hyxy:false,
    ProductId:[],

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var url = 'api/FeijiuApp/GetUserRanks?strRSA=';
    app.getDaoGet(url,function (r) {
      // console.log(r)
      var typel = new Array();
      var hyjgl = new Array();
      var prol = new Array();
      if (r.code == 200) {
        for (var i = r.data.length - 1; i >= 0; i--) {
          typel.push(r.data[i].RankName);
          hyjgl.push(r.data[i].RankPrice)
          prol.push(r.data[i].ProductId)
        }
        that.setData({
          hytypelist:typel,
          hyjglist:hyjgl,
          hyjg:hyjgl[0],
          hyzf:hyjgl[0],
          hytype:typel[0],
          ProductId:prol
        })
      }
    })
  },
  jiaYear:function () {
    var aa = this.data.hyyear+1;
    this.setData({
      hyyear:aa,
      hyzf:this.data.hyjg*aa
    })
  },
  jianYear:function () {
    var aa = this.data.hyyear-1;
    if (aa >= 1) {
      this.setData({
        hyyear:aa,
        hyzf:this.data.hyjg*aa
      })
    }
  },
  chankanSm:function () {
    if (this.data.hyxy) {
      this.setData({
        hyxy:false
      })
    }else{
      this.setData({
        hyxy:true
      })
    }
  },
  bindPickerChange:function (e) {
    var that = this ;
    wx.showActionSheet({
      itemList: that.data.hytypelist,
      success: function (e) {
        that.setData({
          index:e.tapIndex,
          hytype:that.data.hytypelist[e.tapIndex],
          hyjg:that.data.hyjglist[e.tapIndex],
          hyzf:that.data.hyjglist[e.tapIndex],
          hyyear:1
        })        
      }
    }) 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
  Payment:function () {
    var that = this;
    wx.login({
      success: function(res) {
        that.getOpenId(res.code);          
      }

    })
  },
  getOpenId: function(jsCode) {
  
    var that = this
    var api_input = {
      js_code:jsCode
    }
    app.getDaoPost('api/FeijiuApp/Add_DingDan_XCX_GetCode',api_input,function (r) {
    
      var payargs = JSON.parse(r.data);
      if (r.code == 200) {
        that.getPrePayId(payargs.openid);
      }
    })
  },
  getPrePayId:function (openid) {

    var that = this;
    var ss = {
      strKey:'U2FsdGVX8mLqSLnHYjvqkX1MLHBt41W8tVTY1L'
    }
    app.getDaoPostOld('api/FeijiuApp/PostReqTKInfo',ss,function (r) {
     
      if (r.code == 200) {
        var FjLoginLog = wx.getStorageSync('FjLoginLog');         
        var api_input={
          type:2,
          RankName:that.data.hytype,
          RankPrice:that.data.hyzf,
          ProductId:that.data.ProductId[that.data.index],
          strRSA:app.aesAdd(r.data.code+'R1mdL4g8uffdPlRxaL4gpL', 'feijiuwangappxin','0311871066668631'),
          UID:FjLoginLog.UID,
          Year:that.data.hyyear,
          OpenType:1,
          HDID:0,
          openid:openid,
          strTemp:"1"
        } 
   
        app.getDaoPost('api/FeijiuApp/Add_DingDan_XCX',api_input,function (r) {
        
// {"appid":"wx6c82085df9215a6b","partnerid":"1483212472","prepayid":"wx10154703566601f0b09d95d01779695772","package":"Sign=WXPay","noncestr":"cbEWItDOIbKQUMDL","timestamp":"1536565601","sign":"BE66E2E528F844583C51963E6C53D69F"}

          if (r.code == 200) {
           
            var payargs = JSON.parse(r.data);
           
            wx.requestPayment({
              timeStamp: payargs.timestamp,
              nonceStr: payargs.noncestr,
              package: 'prepay_id='+payargs.prepayid,
              signType: 'MD5',
              paySign: payargs.sign,
              success:function (res) {
             
              }
            })
          }
        })                   
      }
    })

    
    
    // var api_input={
    //   type:2,
    //   RankName:this.data.hytype,
    //   RankPrice:this.data.hyzf,
    //   ProductId:this.data.ProductId[this.data.index],
    //   strRSA:app.data.strRSA,
    //   UID:FjLoginLog.UID,
    //   Year:this.data.hyyear,
    //   OpenType:1,
    //   HDID:0,
    //   openid:openid
    // }
    // console.log(api_input);
    // app.getDaoPostOld('api/FeijiuApp/Add_DingDan_XCX',api_input,function (r) {
    //   console.log(r)
    //   // if (r.code == 200) {
    //   //   console.log(r)
    //   //   var payargs = JSON.parse(r.data);
    //   //   console.log(payargs);
    //   //   // wx.requestPayment({
    //   //   //   timeStamp: payargs.timestamp,
    //   //   //   nonceStr: payargs.noncestr,
    //   //   //   package: payargs.package,
    //   //   //   signType: 'MD5',
    //   //   //   paySign: payargs.sign,
    //   //   //   success:function (res) {
    //   //   //     console.log(res)
    //   //   //   }
    //   //   // })
    //   // }
    // })    
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})